package demo

import (
	"fmt"
	"gitee.com/injoyai/goutil/string/logs/v1"
	"github.com/google/gopacket"
	"github.com/google/gopacket/layers"
	"github.com/google/gopacket/pcap"
	"time"
)

var (
	device       string = "\\Device\\NPF_{EEB0B06F-A048-43C9-A6C1-641E73F318FE}"
	snapshot_len int32  = 1024
	promiscuous  bool   = false
	err          error
	timeout      time.Duration = 30 * time.Second
	handle       *pcap.Handle
)

func init() {
	return
	path := "F:/aa.pcap"

	devices, err := pcap.FindAllDevs()
	if err != nil {
		logs.Err(err)
	}

	for _, device := range devices {
		logs.Debug(device.Addresses)
		logs.Debug(device.Name)
	}

	handler, err := pcap.OpenOffline(path)
	if err != nil {
		logs.Err(err)
	}
	_ = handler
}

func Demo() {
	netCard := ""
	_ = netCard
	fmt.Print("Hello World\n")

	handle, err = pcap.OpenLive(device, snapshot_len, true, timeout)
	logs.PrintErr(err)
	//handle.SetBPFFilter("HTTP")
	defer handle.Close()
	//handle.WritePacketData()

	packetSource := gopacket.NewPacketSource(handle, handle.LinkType())

	for packet := range packetSource.Packets() {
		logs.Debug(packet.Metadata().CaptureLength, packet.Metadata().Length, packet.Metadata().Truncated)
		if layer := packet.Layer(layers.LayerTypeTCP); layer != nil {
			logs.Debug(string(layer.LayerPayload()))
		}
	}
}
